#include "CSpaceStationState.h"
#include "CCyberSpaceState.h"
#include "CMainMenuState.h"
#include "CPauseState.h"
#include "COptionsState.h"
#include "Game Objects/CPlayer.h"
#include "Game Objects/CCamera.h"
#include "Game Objects\CNode.h"
#include "Game Objects\CTeleporter.h"
#include "Game Objects\CDrop.h"
#include "Messaging\CSGP_EventSystem.h"
#include "CAI.h"
#include "CAnimationManager.h"
#include "console.h"
#include "CDoor.h"
#include "SGD_Math.h"
#include "CWinState.h"


CSpaceStationState::CSpaceStationState(void)
{
	m_pTM = NULL;
	m_pD3D = NULL;

	m_nPercentLoaded = 0;
}

CSpaceStationState::CSpaceStationState(const CSpaceStationState&)
{

}

CSpaceStationState& CSpaceStationState::operator=(const CSpaceStationState&)
{
	return *this;
}

CSpaceStationState::~CSpaceStationState(void)
{

}

CSpaceStationState* CSpaceStationState::GetInstance(void)
{
	static CSpaceStationState instance;
	return &instance;
}

void CSpaceStationState::Enter(void)
{
	CSGP_MessageSystem::GetInstance()->InitMessageSystem(CSpaceStationState::MessageProcess);
	ObjectFactory<string, CBase>::GetInstance()->RegisterClassType<CDrop>("CDrop");

	m_pTM	= CSGD_TextureManager::GetInstance();
	m_pD3D	= CSGD_Direct3D::GetInstance();

	if(!m_pPlayer)
	{
		m_pPlayer = CPlayer::GetInstance();
		m_pPlayer->SetPosX(200);
		m_pPlayer->SetPosY(150);
		m_pPlayer->SetVelX(0.0f);
		m_pPlayer->SetVelY(0.0f);
		m_pPlayer->SetType(CBase::OBJ_PLAYER);

		ObjectManager::GetInstance()->AddObject(m_pPlayer);
	}

	if(!m_pCamera)
	{
		m_pCamera = CCamera::GetInstance();
	}
	// Load assets
	m_nLoadingID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/Art Assets/cyberScape_logo.png");
	DisplayLoading();
	m_nBackgroundID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_SSBackground.png");
	DisplayLoading();
	m_nMusicID = CSGD_FModManager::GetInstance()->LoadSound("Resource/Sound/SGP_SSMusic.mp3");
	DisplayLoading();

	// Set the music volume
	// Load the options
	COptionsState::GetInstance()->Load("Options.xml");
	// Play the music
	CSGD_FModManager::GetInstance()->SetVolume(m_nMusicID, COptionsState::GetInstance()->GetVolume());
	CSGD_FModManager::GetInstance()->PlaySoundA(m_nMusicID);
	// Load a map
	initnodes();

	map.getNodes()->clear();
	map.Load(".\\Resource\\XML\\space.xml");
	map.getNodes()->operator[](0)->EnterNode();

	

	
	//Create Health Update
	//CSGP_MessageSystem::GetInstance()->SendMessageA(new CCreateHealthDropMessage(m_pPlayer));

	//load animations
	DownAnimationID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/PlayerMovingDownSpaceState.xml",D3DCOLOR_XRGB(34,190,34));
	UpAnimationID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/PlayerMovingUpSpaceState.xml",D3DCOLOR_XRGB(34,190,34));
	LeftAnimationID = CAnimationManager::GetInstance()->Load("Resource/Graphics/Animations/PlayerMovingLeftSpaceState.xml",D3DCOLOR_XRGB(34,190,34));
	

	Movingdown = false;
	Movingup = false;
	Movingright = false;
	Movingleft = false;

	iswon = false;
	m_bWonTheGame = false;
	
	// Create speed powerup
	CSGP_MessageSystem::GetInstance()->SendMessageA(new CCreateSpeedUpgradeMessage(m_pPlayer));

	//Create Money powerUp
	CSGP_MessageSystem::GetInstance()->SendMessageA(new CCreateMoneyUpgradeMessage(m_pPlayer));

	// Create fire rate powerup
	CSGP_MessageSystem::GetInstance()->SendMessageA(new CCreateFRUpgradeMessage(m_pPlayer));

	CSGP_MessageSystem::GetInstance()->SendMessageA(new CCreateNothingUpgradeMessage(m_pPlayer));

}

void CSpaceStationState::DisplayLoading(void)
{
	m_pD3D->Clear(0, 0, 0);
	m_pD3D->DeviceBegin();
	m_pD3D->SpriteBegin();

	m_pTM->Draw(m_nLoadingID, 0, 0, 0.67f, 0.6f);

	m_pD3D->SpriteEnd();
	m_pD3D->DeviceEnd();
	m_pD3D->Present();
}

bool CSpaceStationState::Input(void)
{
	// Movement keys
	if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_W) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_UP))
	{
		Movingup = true;
		m_pPlayer->SetVelY(-200.0f);
	}
	else
		Movingup = false;

	if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_S) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_DOWN))
	{
		Movingdown = true;
		m_pPlayer->SetVelY(200.0f);
	}
	else
		Movingdown = false;

	if(!(CSGD_DirectInput::GetInstance()->KeyDown(DIK_W) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_UP)) && !(CSGD_DirectInput::GetInstance()->KeyDown(DIK_S) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_DOWN)))
	{

		m_pPlayer->SetVelY(0.0f);
	}

	if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_A) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_LEFT))
	{
		Movingleft = true;
		m_pPlayer->SetVelX(-200.0f);
	}
	else
		Movingleft = false;

	if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_D) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_RIGHT))
	{
		Movingright = true;
		m_pPlayer->SetVelX(200.0f);
	}
	else Movingright = false;

	if(!(CSGD_DirectInput::GetInstance()->KeyDown(DIK_A) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_LEFT)) && !(CSGD_DirectInput::GetInstance()->KeyDown(DIK_D) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_RIGHT)))
	{
		m_pPlayer->SetVelX(0.0f);
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0))
	{

		vector<console*>* cons = map.getcurNode()->getcons();
		for(unsigned int i = 0; i < (*cons).size(); i++)
		{
			RECT tempr;
			if(IntersectRect(&tempr, &m_pPlayer->GetCollisionRect(), &(*cons)[i]->GetRect()))
			{
				enterposx = (int)CPlayer::GetInstance()->GetPosX();
				enterposy = (int)CPlayer::GetInstance()->GetPosY();
				Loadmap(7);
				CCyberSpaceState::GetInstance()->setmap(&finalmap);

				// Remove all objects in object manager
				ObjectManager::GetInstance()->RemoveAllObjects();

				// Stop the music
				if(CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nMusicID))
					CSGD_FModManager::GetInstance()->StopSound(m_nMusicID);

				CGame::GetInstance()->PushState(CCyberSpaceState::GetInstance());
				currentstate = false;
			}
		}
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_ESCAPE) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(6))
	{
		CGame::GetInstance()->PushState(CPauseState::GetInstance());
	}
	if((CSGD_DirectInput::GetInstance()->KeyDown(DIK_RMENU)&&CSGD_DirectInput::GetInstance()->KeyPressed(DIK_TAB ))
		||((CSGD_DirectInput::GetInstance()->KeyDown(DIK_LMENU)&&CSGD_DirectInput::GetInstance()->KeyPressed(DIK_TAB ))))
	{
		CGame::GetInstance()->PushState(CPauseState::GetInstance());
	}

	return true;
}

void CSpaceStationState::Update(float fElapsedTime)
{
	CSGD_FModManager::GetInstance()->SetVolume(m_nMusicID, COptionsState::GetInstance()->GetVolume());
	currentstate = true;
	// Update objects in object manager
	ObjectManager::GetInstance()->UpdateObjects(fElapsedTime);

	ObjectManager::GetInstance()->CheckCollisions();

	m_pCamera->Update(m_pPlayer);

	map.update(fElapsedTime);

	m_pPlayer->Update(fElapsedTime);


	CSGP_EventSystem::GetInstance()->ProcessEvents();
	CSGP_MessageSystem::GetInstance()->ProcessMessages();

	//Update the Animations

	if(Movingdown)
		CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, DownAnimationID);
	else if(Movingup)
		CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, UpAnimationID);
	else if(Movingleft)
		CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, LeftAnimationID);
	else if(Movingright)
		CAnimationManager::GetInstance()->UpdateAnimation(fElapsedTime, LeftAnimationID);


	if(iswon)
	{
		vector<CDoor*>* doors = map.getcurNode()->getdoors();
		CDoor* door;
		float prevint = -1;
		float curint = -1;
		for(unsigned int i = 0; i < (*doors).size();i++)
		{
			float x = (*doors)[i]->GetPosX() - CPlayer::GetInstance()->GetPosX();
			float y = (*doors)[i]->GetPosY() - CPlayer::GetInstance()->GetPosY();
			prevint = sqrtf((x*x) + (y*y));
			if(curint == -1)
			{
				door = (*doors)[i];
				curint = prevint;
			}
			else if(prevint < curint)
			{
				curint = prevint;
				door = (*doors)[i];
			}

		}
		door->open();
		iswon = false;
	}
	if(m_pPlayer->GetPosY()<0)
	{
		CGame::GetInstance()->PushState(CWinState::GetInstance());
		m_bWonTheGame = true;
	}
}

void CSpaceStationState::Render(void)
{
	int camOffsetX = CCamera::GetInstance()->GetOffsetX();
	int camOffsetY = CCamera::GetInstance()->GetOffsetY();

	CSGD_TextureManager::GetInstance()->Draw(m_nBackgroundID, 0, 0, 0.65f, 0.95f);

	map.render();

	m_pPlayer->Render();

	m_rLevelOne.left = 137;
	m_rLevelOne.top = 80;
	m_rLevelOne.right = m_rLevelOne.left + 40;
	m_rLevelOne.bottom = m_rLevelOne.top + 40;

	//CSGD_Direct3D::GetInstance()->DrawRect(m_rLevelOne, 255, 0, 0);
	RECT drawRect = m_pPlayer->GetCollisionRect();
	drawRect.top -= CCamera::GetInstance()->GetOffsetY();
	drawRect.bottom -= CCamera::GetInstance()->GetOffsetY();
	drawRect.left -= CCamera::GetInstance()->GetOffsetX();
	drawRect.right -= CCamera::GetInstance()->GetOffsetX();
	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();

	if(Movingdown)
		CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, DownAnimationID);
	else if(Movingup)
		CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, UpAnimationID);
	else if(Movingleft)
		CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, LeftAnimationID);
	else if(Movingright)
		CAnimationManager::GetInstance()->Play(drawRect.left +(drawRect.right - drawRect.left), drawRect.top,-1.0f,1.0f, LeftAnimationID);

	else if(!(Movingdown||Movingup||Movingleft||Movingright))
	{
		CAnimationManager::GetInstance()->Play(drawRect.left, drawRect.top,1.0f,1.0f, DownAnimationID);
	}


	ObjectManager::GetInstance()->RenderObjects();
}

void CSpaceStationState::Exit(void)
{
	ObjectManager::GetInstance()->RemoveAllObjects();

	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBackgroundID);
	map.killmap();
	finalmap.killmap();
}

void CSpaceStationState::MessageProcess(CBaseMessage* pMsg)
{
	switch(pMsg->GetMsgID())
	{
	case MSG_CREATE_SPEED_UPGRADE:
		{
			CCreateSpeedUpgradeMessage* pSpeedMess = (CCreateSpeedUpgradeMessage*)pMsg;
			CDrop* pDrop = (CDrop*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CDrop");

			pDrop->SetPosX(100);//pSpeedMess->GetBase()->GetPosX());
			pDrop->SetPosY(200);//pSpeedMess->GetBase()->GetPosY());
			pDrop->SetVelX(0.0f);
			pDrop->SetVelY(0.0f);
			pDrop->SetWidth(64);
			pDrop->SetHeight(64);
			pDrop->SetType(CBase::OBJ_PICKUP);
			pDrop->SetPickupType(CDrop::SPEED);
			ObjectManager::GetInstance()->AddObject(pDrop);
			pDrop->Release();
		}
		break;
	case MSG_CREATE_FR_UPGRADE:
		{
			CCreateFRUpgradeMessage* pFireMess = (CCreateFRUpgradeMessage*)pMsg;
			CDrop* pDrop = (CDrop*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CDrop");

			pDrop->SetPosX(CPlayer::GetInstance()->GetPosX()+200);
			pDrop->SetPosY(CPlayer::GetInstance()->GetPosY());
			pDrop->SetVelX(0.0f);
			pDrop->SetVelY(0.0f);
			pDrop->SetWidth(64);
			pDrop->SetHeight(64);
			pDrop->SetType(CBase::OBJ_PICKUP);
			pDrop->SetPickupType(CDrop::FIRE_RATE);
			ObjectManager::GetInstance()->AddObject(pDrop);
			pDrop->Release();
		}
		break;
		case MSG_CREATE_MONEY_UPGRADE:
		{
			CCreateMoneyUpgradeMessage* pMoneyMess = (CCreateMoneyUpgradeMessage*)pMsg;
			CDrop* pDrop = (CDrop*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CDrop");

			pDrop->SetPosX(300);
			pDrop->SetPosY(300);
			pDrop->SetVelX(0.0f);
			pDrop->SetVelY(0.0f);
			pDrop->SetWidth(64);
			pDrop->SetHeight(64);
			pDrop->SetType(CBase::OBJ_PICKUP);
			pDrop->SetPickupType(CDrop::MONEY);
			ObjectManager::GetInstance()->AddObject(pDrop);
			pDrop->Release();
		}
		break;
		case MSG_CREATE_NOTHING_UPGRADE:
		{
			CCreateMoneyUpgradeMessage* pMoneyMess = (CCreateMoneyUpgradeMessage*)pMsg;
			CDrop* pDrop = (CDrop*)ObjectFactory<string, CBase>::GetInstance()->CreateObject("CDrop");

			pDrop->SetType(CBase::OBJ_PICKUP);
			pDrop->SetPickupType(CDrop::MAX_UPGRADE);
			ObjectManager::GetInstance()->AddObject(pDrop);
			pDrop->Release();

		}
		break;
	case MSG_DESTROY_DROP:
		{
			CDestroyDropMessage* pDropDest = (CDestroyDropMessage*)pMsg;
			ObjectManager::GetInstance()->RemoveObject(pDropDest->GetDrop());
		}
		break;
	}
}


void CSpaceStationState::initnodes()
{
	std::string root;
	CMap tempm;
	for(int i = 0; i < NUM_XML_FILES; i++)
	{
		nodeplaceholders temp;
		char buf[20];
		root.clear();
		root = ".\\Resource\\XML\\";
		sprintf_s(buf, "%i.xml", i);
		
		root.append(buf);
		tempm.Load(root.c_str());
		temp.numteles = tempm.getNodes()->operator[](tempm.getNodes()->size() - 1)->getTeles()->size();
		temp.filename = root;
		if(temp.numteles == 2)
			tele2.push_back(temp);
		if(temp.numteles == 3)
		{
			tele3.push_back(temp);
		}
		if(temp.numteles == 4)
			tele4.push_back(temp);
	}
}
void CSpaceStationState::Loadmap(const int numnodes)
{
	finalmap.getNodes()->clear();
	if(numnodes == 4)
	{
		finalmap.Load(".\\Resource\\XML\\player2.xml");
		finalmap.Load(tele3[rand() % tele3.size()].filename.c_str());
		finalmap.Load(tele3[rand() % tele3.size()].filename.c_str());
		finalmap.Load(".\\Resource\\XML\\ai2.xml");

		//vector<CNode*> nodes = finalmap.getNodes();
		//vector<CTeleporter*>* teles = 
		finalmap.getNodes()->operator[](0)->getTeles()->operator[](0)->setLink(finalmap.getNodes()->operator[](1)->getTeles()->operator[](0));
		finalmap.getNodes()->operator[](0)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](2)->getTeles()->operator[](0));
		finalmap.getNodes()->operator[](2)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](1)->getTeles()->operator[](1));
		finalmap.getNodes()->operator[](3)->getTeles()->operator[](0)->setLink(finalmap.getNodes()->operator[](1)->getTeles()->operator[](2));
		finalmap.getNodes()->operator[](3)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](2)->getTeles()->operator[](2));
		return;
	}
	if(numnodes == 5)
	{
		finalmap.Load(".\\Resource\\XML\\player3.xml");
		
		finalmap.Load(tele3[rand() % tele3.size()].filename.c_str());
		finalmap.Load(tele4[rand() % tele4.size()].filename.c_str());
		finalmap.Load(tele3[rand() % tele3.size()].filename.c_str());
		finalmap.Load(".\\Resource\\XML\\ai3.xml");

		finalmap.getNodes()->operator[](0)->getTeles()->operator[](0)->setLink(finalmap.getNodes()->operator[](1)->getTeles()->operator[](0));
		finalmap.getNodes()->operator[](0)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](2)->getTeles()->operator[](0));
		finalmap.getNodes()->operator[](0)->getTeles()->operator[](2)->setLink(finalmap.getNodes()->operator[](3)->getTeles()->operator[](0));

		finalmap.getNodes()->operator[](1)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](2)->getTeles()->operator[](1));
		finalmap.getNodes()->operator[](3)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](2)->getTeles()->operator[](2));
		finalmap.getNodes()->operator[](4)->getTeles()->operator[](0)->setLink(finalmap.getNodes()->operator[](1)->getTeles()->operator[](2));
		finalmap.getNodes()->operator[](4)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](2)->getTeles()->operator[](3));
		finalmap.getNodes()->operator[](4)->getTeles()->operator[](2)->setLink(finalmap.getNodes()->operator[](3)->getTeles()->operator[](2));
		return;

	}
	if(numnodes == 6)
	{
		finalmap.Load(".\\Resource\\XML\\player2.xml");
		finalmap.Load(tele4[rand() % tele4.size()].filename.c_str());
		finalmap.Load(tele4[rand() % tele4.size()].filename.c_str());
		finalmap.Load(tele4[rand() % tele4.size()].filename.c_str());
		finalmap.Load(tele4[rand() % tele4.size()].filename.c_str());
		finalmap.Load(".\\Resource\\XML\\ai2.xml");

		finalmap.getNodes()->operator[](0)->getTeles()->operator[](0)->setLink(finalmap.getNodes()->operator[](1)->getTeles()->operator[](0));
		finalmap.getNodes()->operator[](0)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](2)->getTeles()->operator[](0));

		finalmap.getNodes()->operator[](1)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](2)->getTeles()->operator[](1));
		finalmap.getNodes()->operator[](1)->getTeles()->operator[](2)->setLink(finalmap.getNodes()->operator[](4)->getTeles()->operator[](0));
		finalmap.getNodes()->operator[](1)->getTeles()->operator[](3)->setLink(finalmap.getNodes()->operator[](3)->getTeles()->operator[](0));

		finalmap.getNodes()->operator[](2)->getTeles()->operator[](2)->setLink(finalmap.getNodes()->operator[](3)->getTeles()->operator[](1));
		finalmap.getNodes()->operator[](2)->getTeles()->operator[](3)->setLink(finalmap.getNodes()->operator[](4)->getTeles()->operator[](1));

		finalmap.getNodes()->operator[](3)->getTeles()->operator[](2)->setLink(finalmap.getNodes()->operator[](4)->getTeles()->operator[](2));

		finalmap.getNodes()->operator[](5)->getTeles()->operator[](0)->setLink(finalmap.getNodes()->operator[](3)->getTeles()->operator[](3));
		finalmap.getNodes()->operator[](5)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](4)->getTeles()->operator[](3));
		return;


	}
	if(numnodes == 7)
	{
		finalmap.Load(".\\Resource\\XML\\player2.xml");
		finalmap.Load(tele2[rand() % tele2.size()].filename.c_str());
		finalmap.Load(tele2[rand() % tele2.size()].filename.c_str());
		finalmap.Load(tele4[rand() % tele4.size()].filename.c_str());
		finalmap.Load(tele2[rand() % tele2.size()].filename.c_str());
		finalmap.Load(tele2[rand() % tele2.size()].filename.c_str());
		finalmap.Load(".\\Resource\\XML\\ai2.xml");

		finalmap.getNodes()->operator[](0)->getTeles()->operator[](0)->setLink(finalmap.getNodes()->operator[](1)->getTeles()->operator[](0));
		finalmap.getNodes()->operator[](0)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](2)->getTeles()->operator[](0));


		finalmap.getNodes()->operator[](1)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](3)->getTeles()->operator[](0));
		finalmap.getNodes()->operator[](2)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](3)->getTeles()->operator[](1));

		finalmap.getNodes()->operator[](4)->getTeles()->operator[](0)->setLink(finalmap.getNodes()->operator[](3)->getTeles()->operator[](2));
		finalmap.getNodes()->operator[](5)->getTeles()->operator[](0)->setLink(finalmap.getNodes()->operator[](3)->getTeles()->operator[](3));

		finalmap.getNodes()->operator[](6)->getTeles()->operator[](0)->setLink(finalmap.getNodes()->operator[](4)->getTeles()->operator[](1));
		finalmap.getNodes()->operator[](6)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](5)->getTeles()->operator[](1));

		return;
	}
	if(numnodes == 8)
	{
		finalmap.Load(".\\Resource\\XML\\player2.xml");
		finalmap.Load(tele2[rand() % tele2.size()].filename.c_str());
		finalmap.Load(tele2[rand() % tele2.size()].filename.c_str());
		finalmap.Load(tele3[rand() % tele3.size()].filename.c_str());
		finalmap.Load(tele3[rand() % tele3.size()].filename.c_str());
		finalmap.Load(tele2[rand() % tele2.size()].filename.c_str());
		finalmap.Load(tele2[rand() % tele2.size()].filename.c_str());
		finalmap.Load(".\\Resource\\XML\\ai2.xml");

		finalmap.getNodes()->operator[](0)->getTeles()->operator[](0)->setLink(finalmap.getNodes()->operator[](1)->getTeles()->operator[](0));
		finalmap.getNodes()->operator[](0)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](2)->getTeles()->operator[](0));

		finalmap.getNodes()->operator[](1)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](3)->getTeles()->operator[](0));
		finalmap.getNodes()->operator[](2)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](4)->getTeles()->operator[](0));

		finalmap.getNodes()->operator[](3)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](4)->getTeles()->operator[](1));
		finalmap.getNodes()->operator[](3)->getTeles()->operator[](2)->setLink(finalmap.getNodes()->operator[](5)->getTeles()->operator[](0));

		finalmap.getNodes()->operator[](4)->getTeles()->operator[](2)->setLink(finalmap.getNodes()->operator[](6)->getTeles()->operator[](0));

		finalmap.getNodes()->operator[](7)->getTeles()->operator[](0)->setLink(finalmap.getNodes()->operator[](5)->getTeles()->operator[](1));
		finalmap.getNodes()->operator[](7)->getTeles()->operator[](1)->setLink(finalmap.getNodes()->operator[](6)->getTeles()->operator[](1));

		return;

	}	
}

CNode* CSpaceStationState::getnode()
{
	return map.getcurNode();
}